package com.idsmanager.oauth.infrastructure.mongodb;


import com.idsmanager.commons.utils.mongo.AbstractMongoSupport;
import com.idsmanager.oauth.domain.oauth.AccessTokenRepository;
import com.idsmanager.oauth.domain.oauth.AccessToken;
import com.idsmanager.oauth.domain.oauth.RefreshToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("accessTokenRepositoryMongoDB")
public class AccessTokenRepositoryMongoDB extends AbstractMongoSupport implements AccessTokenRepository {
    @Autowired
    private MongoOperations mongoTemplate;


    @Override
    public void saveAccessToken(AccessToken accessToken) {
        mongoTemplate.save(accessToken);
    }

    @Override
    public AccessToken findAccessToken(String tokenId) {
        Query query = createIDQuery(tokenId);
        return mongoTemplate.findOne(query, AccessToken.class);
    }

    @Override
    public void removeAccessToken(String tokenId) {
        final AccessToken accessToken = findAccessToken(tokenId);
        mongoTemplate.remove(accessToken);

    }

    @Override
    public void saveRefreshToken(RefreshToken refreshToken) {
        mongoTemplate.save(refreshToken);
    }

    @Override
    public RefreshToken findRefreshToken(String tokenId) {
        Query query = createIDQuery(tokenId);
        return mongoTemplate.findOne(query, RefreshToken.class);
    }

    @Override
    public void removeRefreshToken(String tokenId) {
        final RefreshToken refreshToken = findRefreshToken(tokenId);
        mongoTemplate.remove(refreshToken);

    }

    @Override
    public void removeAccessTokenByRefreshToken(String refreshToken) {
        final AccessToken accessToken = findAccessTokenByRefreshToken(refreshToken);
        mongoTemplate.remove(accessToken);

    }

    @Override
    public AccessToken findAccessTokenByRefreshToken(String refreshToken) {
        Query query = new Query(Criteria.where("refreshToken").is(refreshToken));
        return mongoTemplate.findOne(query, AccessToken.class);
    }

    @Override
    public AccessToken findAccessTokenByAuthenticationId(String authenticationId) {
        Query query = new Query(Criteria.where("authenticationId").is(authenticationId));
        return mongoTemplate.findOne(query, AccessToken.class);
    }

    @Override
    public List<AccessToken> findAccessTokenByUsername(String username) {
        Query query = new Query(Criteria.where("username").is(username));
        return mongoTemplate.find(query, AccessToken.class);
    }

    @Override
    public List<AccessToken> findAccessTokenByClientId(String clientId) {
        Query query = new Query(Criteria.where("clientId").is(clientId));
        return mongoTemplate.find(query, AccessToken.class);
    }

    @Override
    public List<AccessToken> findAccessTokenByClientIdAndUsername(
            String clientId, String username) {
        Query query = new Query(new Criteria("clientId").is(clientId));
        query.addCriteria(new Criteria("username").is(username));
        return mongoTemplate.find(query, AccessToken.class);
    }

}
